<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2019/11/7
 * Time: 10:20
 */

namespace app\adminapiv3\service;


class Upload extends BaseService
{
    /**
     * @name 文件上传
     * @param $file 需要上传的文件
     * @param $path 上传路径
     * @param $extension 文件后缀
     * @param int $max_size 最大文件大小
     * @return array
     * @author wx
     * @date 2019/11/7 10:21
     */
    public function upload($file, $path, $extension, $max_size = 1024 * 1024)
    {

        $path = $path .'/'.date('Ymd').'/';
        $info = $file->validate(['size'=>$max_size, 'ext'=>$extension])->rule('uniqid')->move($path);
        if($info){
            $return_data = [
                'code' => 0,
                'path' => $path,
                'file_name' => $info->getSaveName()
            ];

        }else{
            // 上传失败获取错误信息
            $return_data = [
                'code' => $this->CODE_FAIL,
                'msg' => $file->getError()
            ];
        }

        return $return_data;
    }
}